#!/usr/bin/env python
# -*- coding: utf-8 -*-

from fytion import *
from time import sleep
from getpass import getpass
import code

def exited(fetion, e):
	if e:
		print "Exited: %d, %s" % (e.args[0], e.args[1])
	else:
		print "Exited normally"
	

def get_msg(fetion, data):
	c = fetion.get_contact(uri=data['from'])
	if c:
		f = c['name']
	else:
		f = data['from']
	print "[%s] Got message from %s: %s" % (data['timestamp'], f, data['body'])

def main():
	fetion = fytion()
	phone = raw_input("Phone number: ")
	password = getpass("Password: ")
	fetion.init(phoneNo = phone, password = password)
	fetion.login('400')
	fetion.background_tick()
	fetion.exitCallback = exited
	fetion.msgCallback = get_msg

	code.interact(banner="""Fytion object: fetion
Try: fetion.send_msg('message body', phone='phone#')
To exit, do fetion.exit() before exit()""", local={'fetion':fetion})
	
if __name__ == "__main__":
	main()
	
